home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / initramfs-tools / hook-functions next >
Encoding:
Text File  |  2012-09-21  |  13.8 KB  |  531 lines

  1. # -*- shell-script -*-
  2.  
  3. catenate_cpiogz() {
  4.     # Sanity check
  5.     if [ ! -e "${1}" ]; then
  6.         echo "W: catenate_cpiogz: arg1='${1}' does not exist." >&2
  7.         return
  8.     fi
  9.  
  10.     cat "${1}" >>"${__TMPCPIOGZ}"
  11. }
  12.  
  13. # force_load module [args...]
  14. force_load()
  15. {
  16.     manual_add_modules "$1"
  17.     echo "${@}" >>"${DESTDIR}/conf/modules"
  18. }
  19.  
  20. # Takes a file containing a list of modules to be added as an
  21. # argument, figures out dependancies, and adds them.
  22. #
  23. # Input file syntax:
  24. #
  25. #   # comment
  26. #   modprobe_module_name [args ...]
  27. #   [...]
  28. #
  29. add_modules_from_file()
  30. {
  31.     # Sanity check
  32.     if [ ! -e "${1}" ]; then
  33.         echo "W: add_modules_from_file: arg1='${1}' does not exist." >&2
  34.         return
  35.     fi
  36.  
  37.     grep '^[^#]' ${1} | while read module args; do
  38.         [ -n "$module" ] || continue
  39.         force_load "${module}" "${args}"
  40.     done
  41. }
  42.  
  43. # Add dependent modules + eventual firmware
  44. manual_add_modules()
  45. {
  46.     local prefix kmod options firmware
  47.  
  48.     if [ $# -eq 0 ]; then
  49.         return
  50.     fi
  51.  
  52.     modprobe --all --set-version="${version}" --ignore-install --quiet --show-depends "$@" |
  53.     while read prefix kmod options ; do
  54.         if [ "${prefix}" != "insmod" ]; then
  55.             continue
  56.         fi
  57.  
  58.         # Prune duplicates
  59.         if [ -e "${DESTDIR}/${kmod}" ]; then
  60.             continue
  61.         fi
  62.  
  63.         install -Dpm 644 "$kmod" "${DESTDIR}/$kmod"
  64.         if [ "${verbose}" = "y" ]; then
  65.             echo "Adding module ${kmod}"
  66.         fi
  67.  
  68.         # Add required firmware
  69.         for firmware in $(modinfo -F firmware "${kmod}"); do
  70.             if [ -e "${DESTDIR}/lib/firmware/${firmware}" ] \
  71.             || [ -e "${DESTDIR}/lib/firmware/${version}/${firmware}" ]; then
  72.                 continue
  73.             fi
  74.  
  75.             # Only print warning for missing fw of loaded module
  76.             # or forced loaded module
  77.             if [ ! -e "/lib/firmware/${firmware}" ] \
  78.             && [ ! -e "/lib/firmware/${version}/${firmware}" ] ; then
  79.                 # Only warn about missing firmware if
  80.                 # /proc/modules exists
  81.                 if [ ! -e /proc/modules ] ; then
  82.                     continue
  83.                 fi
  84.  
  85.                 kmod_modname="${kmod##*/}"
  86.                 kmod_modname="${kmod_modname%.ko}"
  87.                 if grep -q "^$kmod_modname\\>" /proc/modules "${CONFDIR}/modules"; then
  88.                     echo "W: Possible missing firmware /lib/firmware/${firmware} for module $(basename ${kmod} .ko)" >&2
  89.                 fi
  90.                 continue
  91.             fi
  92.  
  93.             if [ -e "/lib/firmware/${version}/${firmware}" ]; then
  94.                 copy_exec "/lib/firmware/${version}/${firmware}"
  95.             else
  96.                 copy_exec "/lib/firmware/${firmware}"
  97.             fi
  98.             if [ "${verbose}" = "y" ]; then
  99.                 echo "Adding firmware ${firmware}"
  100.             fi
  101.         done
  102.     done
  103. }
  104.  
  105. # $1 = file to copy to ramdisk
  106. # $2 (optional) Name for the file on the ramdisk
  107. # Location of the image dir is assumed to be $DESTDIR
  108. # We never overwrite the target if it exists.
  109. copy_exec() {
  110.     local src target x nonoptlib
  111.     local libname dirname
  112.  
  113.     src="${1}"
  114.     target="${2:-$1}"
  115.  
  116.     [ -f "${src}" ] || return 1
  117.  
  118.     if [ -d "${DESTDIR}/${target}" ]; then
  119.         # check if already copied
  120.         [ -e "${DESTDIR}/$target/${src##*/}" ] && return 0
  121.     else
  122.         [ -e "${DESTDIR}/$target" ] && return 0
  123.         #FIXME: inst_dir
  124.         mkdir -p "${DESTDIR}/${target%/*}"
  125.     fi
  126.  
  127.     [ "${verbose}" = "y" ] && echo "Adding binary ${src}"
  128.     cp -pL "${src}" "${DESTDIR}/${target}"
  129.  
  130.     # Copy the dependant libraries
  131.     for x in $(ldd "${src}" 2>/dev/null | sed -e '
  132.         /\//!d;
  133.         /linux-gate/d;
  134.         /=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/};
  135.         s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do
  136.  
  137.         # Try to use non-optimised libraries where possible.
  138.         # We assume that all HWCAP libraries will be in tls,
  139.         # sse2, vfp or neon.
  140.         nonoptlib=$(echo "${x}" | sed -e 's#/lib/\([^/]*/\)\?\(tls\|i686\|sse2\|neon\|vfp\).*/\(lib.*\)#/lib/\1\3#')
  141.         nonoptlib=$(echo "${nonoptlib}" | sed -e 's#-linux-gnu/\(tls\|i686\|sse2\|neon\|vfp\).*/\(lib.*\)#-linux-gnu/\2#')
  142.  
  143.         if [ -e "${nonoptlib}" ]; then
  144.             x="${nonoptlib}"
  145.         fi
  146.  
  147.         libname=$(basename "${x}")
  148.         dirname=$(dirname "${x}")
  149.  
  150.         # FIXME inst_lib
  151.         mkdir -p "${DESTDIR}/${dirname}"
  152.         if [ ! -e "${DESTDIR}/${dirname}/${libname}" ]; then
  153.             cp -pL "${x}" "${DESTDIR}/${dirname}"
  154.             [ "${verbose}" = "y" ] && echo "Adding library ${x}" || true
  155.         fi
  156.     done
  157. }
  158.  
  159. # Copy entire subtrees to the initramfs
  160. copy_modules_dir()
  161. {
  162.     local kmod exclude
  163.     local modules=
  164.     local dir="$1"
  165.     shift
  166.  
  167.     if ! [ -d "${MODULESDIR}/${dir}" ]; then
  168.         return;
  169.     fi
  170.     if [ "${verbose}" = "y" ]; then
  171.         echo "Copying module directory ${dir}"
  172.         if [ $# -ge 1 ]; then
  173.             echo "(excluding $*)"
  174.         fi
  175.     fi
  176.     while [ $# -ge 1 ]; do
  177.         exclude="${exclude:-} -name $1 -prune -o "
  178.         shift
  179.     done
  180.     for kmod in $(find "${MODULESDIR}/${dir}" ${exclude:-} -name '*.ko' -printf '%f\n'); do
  181.         modules="$modules ${kmod%.ko}"
  182.     done
  183.     manual_add_modules $modules
  184. }
  185.  
  186. # walk /sys for relevant modules
  187. sys_walk_mod_add()
  188. {
  189.     local driver_path module
  190.     device_path="$1"
  191.  
  192.     while [ "${device_path}" != "/sys" ]; do
  193.         sys_walk_modalias ${device_path}
  194.         driver_path="$(readlink -f ${device_path}/driver/module)"
  195.         if [ -e "$driver_path" ]; then
  196.             module="$(basename $(readlink -f $driver_path))"
  197.             if [ -n "${module}" ]; then
  198.                 force_load "${module}"
  199.             fi
  200.         fi
  201.         device_path="$(dirname ${device_path})"
  202.     done
  203. }
  204.  
  205. # walk /sys for relevant modalias
  206. sys_walk_modalias()
  207. {
  208.     local device_path modalias
  209.  
  210.     device_path="$(dirname "${1}")"
  211.     device_path="$(dirname "${device_path}")"
  212.     if [ -e "${device_path}/modalias" ]; then
  213.         modalias=$(cat "${device_path}/modalias")
  214.     fi
  215.  
  216.     if [ -n "${modalias}" ]; then
  217.         force_load "${modalias}"
  218.     fi
  219. }
  220.  
  221. # find and only copy root relevant modules
  222. dep_add_modules()
  223. {
  224.     local block minor root FSTYPE root_dev_path
  225.     local modules=
  226.  
  227.     # require mounted sysfs
  228.     if [ ! -d /sys/devices/ ]; then
  229.         echo "mkinitramfs: MODULES dep requires mounted sysfs on /sys" >&2
  230.         exit 1
  231.     fi
  232.  
  233.     # findout root block device + fstype
  234.     eval "$( mount | while read dev foo mp foo fs opts rest ; do \
  235.             [ "$mp" = "/" ] && [ "$fs" != "rootfs" ] \
  236.         && printf "root=$dev\nFSTYPE=$fs" \
  237.         && break; done)"
  238.  
  239.     # On failure fallback to /proc/mounts if readable
  240.     if [ -z "$root" ] && [ -r /proc/mounts ]; then
  241.         eval "$(while read dev mp fs opts rest ; do \
  242.             [ "$mp" = "/" ] && [ "$fs" != "rootfs" ] \
  243.             && printf "root=$dev\nFSTYPE=$fs"\
  244.             && break; done < /proc/mounts)"
  245.     fi
  246.  
  247.     # recheck root device
  248.     if [ -z "$root" ]; then
  249.         echo "mkinitramfs: failed to determine root device" >&2
  250.         echo "mkinitramfs: workaround is MODULES=most, check:" >&2
  251.         echo "grep -r MODULES /etc/initramfs-tools/" >&2
  252.         echo "" >&2
  253.         echo "Error please report bug on initramfs-tools" >&2
  254.         echo "Include the output of 'mount' and 'cat /proc/mounts'" >&2
  255.         exit 1
  256.     fi
  257.  
  258.     # handle ubifs and return since ubifs root is a char device but
  259.     # most of the commands below only work with block devices.
  260.     if [ "${FSTYPE}" = "ubifs" ]; then
  261.         manual_add_modules "${FSTYPE}"
  262.         return
  263.     fi
  264.  
  265.     if [ "${root}" = "/dev/root" ] ; then
  266.         root="/dev/disk/by-uuid/"$(blkid -o value -s UUID ${root}) 2>/dev/null
  267.     fi
  268.     root="$(readlink -f ${root})"
  269.  
  270.     # do not trust mount, check superblock
  271.     eval "$(/usr/lib/klibc/bin/fstype ${root})"
  272.  
  273.     # check that fstype rootfs recognition
  274.     if [ "${FSTYPE}" = "unknown" ]; then
  275.         FSTYPE=$(blkid -o value -s TYPE "${root}")
  276.         if [  -z "${FSTYPE}" ]; then
  277.             echo "mkinitramfs: unknown fstype on root ${root}" >&2
  278.             echo "mkinitramfs: workaround is MODULES=most" >&2
  279.             echo "Error please report bug on initramfs-tools" >&2
  280.             exit 1
  281.         fi
  282.     fi
  283.  
  284.     # Add rootfs
  285.     modules="$modules ${FSTYPE}"
  286.  
  287.     # lvm or luks root
  288.     if [ "${root#/dev/mapper/}" != "${root}" ] \
  289.         || [ "${root#/dev/dm-}" != "${root}" ]; then
  290.         minor=$((0x$(stat --format "%T" ${root}) % 256))
  291.         block=$(ls -1 /sys/block/dm-${minor}/slaves | head -n 1)
  292.         # lvm on luks or luks on lvm, possibly lvm snapshots
  293.         while [ "${block#dm-}" != "${block}" ]; do
  294.             block=$(ls -1 /sys/block/${block}/slaves | head -n 1)
  295.         done
  296.         # lvm on md or luks on md
  297.         if [ "${block#md}" != "${block}" ]; then
  298.             block=$(sed -ne 's/multipath/[/' -e 's/linear/[/' -e 's/raid[0-9][0-9]*/[/' -e 's/\([hs]d[a-z][a-z]*\)[0-9][0-9]*/\1/g' -e '/^'${block}' :/s/^[^[]*\[ \([^\[]*\)\[.*$/\1/p' </proc/mdstat)
  299.         fi
  300.         # luks or lvm on cciss or ida
  301.         if [ "${block#cciss}" != "${block}" ] \
  302.         || [ "${block#ida}" != "${block}" ]; then
  303.             block="${block%p*}"
  304.         else
  305.             block=${block%%[0-9]*}
  306.         fi
  307.     # md root new naming scheme /dev/md/X
  308.     elif [ "${root#/dev/md/}" != "${root}" ]; then
  309.         root=${root#/dev/md/}
  310.         # strip partion number
  311.         root=${root%%p[0-9]*}
  312.         # drop the partition number only for sdX and hdX devices
  313.         # and keep it for other devices like loop#, dm-# devices
  314.         block=$(sed -ne 's/multipath/[/' -e 's/linear/[/' -e 's/raid[0-9][0-9]*/[/' -e 's/\([hs]d[a-z][a-z]*\)[0-9][0-9]*/\1/g' -e '/^md'$root' :/s/^[^[]*\[ \([^\[]*\)\[.*$/\1/p' </proc/mdstat)
  315.     # md root /dev/mdX
  316.     elif [ "${root#/dev/md}" != "${root}" ]; then
  317.         root=${root#/dev/md}
  318.         # strip partion number
  319.         root=${root%%p[0-9]*}
  320.         # drop the partition number only for sdX and hdX devices
  321.         # and keep it for other devices like loop#, dm-# devices
  322.         block=$(sed -ne 's/multipath/[/' -e 's/linear/[/' -e 's/raid[0-9][0-9]*/[/' -e 's/\([hs]d[a-z][a-z]*\)[0-9][0-9]*/\1/g' -e '/^md'$root' :/s/^[^[]*\[ \([^\[]*\)\[.*$/\1/p' </proc/mdstat)
  323.     # cciss device
  324.     elif [ "${root#/dev/cciss/}" != "${root}" ]; then
  325.         block=${root#/dev/cciss/*}
  326.         block="cciss!${block%p*}"
  327.     # ida device
  328.     elif [ "${root#/dev/ida/}" != "${root}" ]; then
  329.         block=${root#/dev/ida/*}
  330.         block="ida!${block%p*}"
  331.     # loop root /dev/loopX
  332.     elif [ "${root#/dev/loop}" != "${root}" ]; then
  333.         root=${root#/dev/}
  334.         block=$(losetup -a \
  335.             | awk "/${root}/{print substr(\$3, 7, 3); exit}")
  336.     # Xen virtual device /dev/xvdX
  337.     elif [ "${root#/dev/xvd}" != "${root}" ]; then
  338.         block=${root#/dev/}
  339.         # Xen has a mode where only the individual partitions are
  340.         # registered with the kernel as well as the usual full disk
  341.         # with partition table scheme.
  342.         if [ ! -e /sys/block/${block} ] ; then
  343.             block=${block%%[0-9]*}
  344.         fi
  345.     # mmc root /dev/mmcblkXpX
  346.     elif [ "${root#/dev/mmcblk}" != "${root}" ]; then
  347.         block=${root#/dev/}
  348.         block=${block%%p[0-9]*}
  349.  
  350.     # DAC960 - good old mylex raid - root dev format /dev/rd/cXdXpX
  351.     elif [ "${root#/dev/rd/c}" != "${root}" ]; then
  352.         block="rd!c${root#/dev/rd/c}"
  353.         block=${block%%p[0-9]*}
  354.  
  355.     # etherd device
  356.     elif [ "${root#/dev/etherd/}" != "${root}" ]; then
  357.         block=${root#/dev/etherd/*}
  358.         block="etherd!${block%p*}"
  359.     # classical root device
  360.     else
  361.         block=${root#/dev/}
  362.         block=${block%%[0-9]*}
  363.     fi
  364.  
  365.     # Error out if /sys lack block dev
  366.     if [ -z "${block}" ] || [ ! -e /sys/block/${block} ]; then
  367.         echo "mkinitramfs: for root ${root} missing ${block} /sys/block/ entry" >&2
  368.         echo "mkinitramfs: workaround is MODULES=most" >&2
  369.         echo "mkinitramfs: Error please report the bug" >&2
  370.         exit 1
  371.     fi
  372.  
  373.     # sys walk ATA
  374.     root_dev_path=$(readlink -f /sys/block/${block}/device)
  375.     sys_walk_mod_add ${root_dev_path}
  376.  
  377.     # catch old-style IDE
  378.     if [ -e /sys/bus/ide/devices/ ]; then
  379.         sys_walk_modalias ${root_dev_path}
  380.         modules="$modules ide-gd_mod ide-cd"
  381.     fi
  382.  
  383.     if [ -e /sys/bus/scsi/devices/ ]; then
  384.         modules="$modules sd_mod"
  385.     fi
  386.  
  387.     if [ -e /sys/bus/mmc/devices/ ]; then
  388.         modules="$modules mmc_block"
  389.     fi
  390.  
  391.     if [ -e /sys/bus/virtio ] ; then
  392.         modules="$modules virtio_pci"
  393.     fi
  394.  
  395.     if [ -e /sys/bus/i2o/devices/ ]; then
  396.         force_load i2o_block
  397.         force_load i2o_config
  398.     fi
  399.  
  400.     if [ -e /sys/bus/ps3_system_bus/ ]; then
  401.         modules="$modules ps3disk ps3rom ps3-gelic ps3_sys_manager"
  402.     fi
  403.  
  404.     if [ -e /sys/bus/vio/ ]; then
  405.         modules="$modules sunvnet sunvdc"
  406.     fi
  407.  
  408.     manual_add_modules $modules
  409. }
  410.  
  411.  
  412. # The modules "most" classes added per default to the initramfs
  413. auto_add_modules()
  414. {
  415.     local arg
  416.     local modules=
  417.  
  418.     if [ "$#" -eq 0 ] ; then
  419.         set -- base net ide scsi block ata i2o dasd ieee1394 firewire mmc usb_storage
  420.     fi
  421.  
  422.     for arg in "$@" ; do
  423.         case "$arg" in
  424.         base)
  425.             modules="$modules ehci-hcd ohci-hcd uhci-hcd usbhid"
  426.             modules="$modules xhci xhci-hcd"
  427.             modules="$modules hid-apple hid-cherry hid-generic"
  428.             modules="$modules hid-logitech hid-logitech-dj"
  429.             modules="$modules hid-microsoft hid-sunplus"
  430.             modules="$modules btrfs ext2 ext3 ext4 ext4dev "
  431.             modules="$modules isofs jfs nfs reiserfs udf xfs"
  432.             modules="$modules af_packet atkbd i8042 virtio_pci"
  433.         ;;
  434.         net)
  435.             copy_modules_dir kernel/drivers/net \
  436.                 appletalk arcnet bonding can hamradio irda \
  437.                 pcmcia tokenring usb wan wimax wireless
  438.         ;;
  439.         ide)
  440.             copy_modules_dir kernel/drivers/ide
  441.         ;;
  442.         mmc)
  443.             copy_modules_dir kernel/drivers/mmc
  444.         ;;
  445.         scsi)
  446.             copy_modules_dir kernel/drivers/scsi
  447.             modules="$modules mptfc mptsas mptscsih mptspi zfcp"
  448.         ;;
  449.         ata)
  450.             copy_modules_dir kernel/drivers/ata
  451.         ;;
  452.         block)
  453.             copy_modules_dir kernel/drivers/block
  454.         ;;
  455.         ubi)
  456.             modules="$modules deflate zlib lzo ubi ubifs"
  457.         ;;
  458.         ieee1394)
  459.             modules="$modules ohci1394 sbp2"
  460.         ;;
  461.         firewire)
  462.             modules="$modules firewire-ohci firewire-sbp2"
  463.         ;;
  464.         i2o)
  465.             modules="$modules i2o_block"
  466.         ;;
  467.         dasd)
  468.             modules="$modules dasd_diag_mod dasd_eckd_mod dasd_fba_mod"
  469.         ;;
  470.         usb_storage)
  471.             copy_modules_dir kernel/drivers/usb/storage
  472.         ;;
  473.         esac
  474.     done
  475.  
  476.     manual_add_modules $modules
  477. }
  478.  
  479. # 'depmod' only looks at symbol dependencies; there is no way for
  480. # modules to declare explicit dependencies through module information,
  481. # so dependencies on e.g. crypto providers are hidden.  Until this is
  482. # fixed, we need to handle those hidden dependencies.
  483. hidden_dep_add_modules()
  484. {
  485.     local modules=
  486.     for dep in "lib/libcrc32c crc32c" "fs/ubifs/ubifs deflate zlib lzo"; do
  487.         set -- $dep
  488.         if [ -f "${DESTDIR}/lib/modules/${version}/kernel/$1.ko" ]; then
  489.             shift
  490.             modules="$modules $@"
  491.         fi
  492.     done
  493.     manual_add_modules $modules
  494. }
  495.  
  496. # mkinitramfs help message
  497. usage()
  498. {
  499.     cat >&2 << EOF
  500.  
  501. Usage: ${0} [OPTION]... -o outfile [version]
  502.  
  503. Options:
  504.   -c compress    Override COMPRESS setting in initramfs.conf.
  505.   -d confdir    Specify an alternative configuration directory.
  506.   -k        Keep temporary directory used to make the image.
  507.   -o outfile    Write to outfile.
  508.   -r root    Override ROOT setting in initramfs.conf.
  509.  
  510. See mkinitramfs(8) for further details.
  511. EOF
  512.     exit 1
  513.  
  514. }
  515.  
  516. # cache boot scripts order
  517. cache_run_scripts()
  518. {
  519.     DESTDIR=${1}
  520.     scriptdir=${2}
  521.     initdir=${DESTDIR}${scriptdir}
  522.     [ ! -d ${initdir} ] && return
  523.  
  524.     runlist=$(get_prereq_pairs | tsort)
  525.     for crs_x in ${runlist}; do
  526.         [ -f ${initdir}/${crs_x} ] || continue
  527.         echo "${scriptdir}/${crs_x}" >> ${initdir}/ORDER
  528.         echo "[ -e /conf/param.conf ] && . /conf/param.conf" >> ${initdir}/ORDER
  529.     done
  530. }
  531.